package ev;

import java.util.Random;

import TeamPursuit.MensTeamPursuit;
import TeamPursuit.SimulationResult;

public class EA_Team_Men2 implements Team_Men {

	@Override
	public void emulation() {
		boolean [] strategy = {true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false, true, false};
	    int [] power = {550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550, 550};
    	SimulationResult result = null;
        MensTeamPursuit mensTeamPursuit = new MensTeamPursuit();
        
        boolean[] str = new boolean[strategy.length];
        int[] pow = new int[power.length];
        
        
	    int times = 500;
	    int outtime = 100;
	    double finalTime = 1000;
	    double best = 1000;
        int index, pValue;
        Random r = new Random();
        
        while(outtime>0){
        	finalTime = 1000;
			while(times>0){
				index = r.nextInt(power.length);
				pValue = power[index];
				power[index] = r.nextInt(1001)+200;
		    	try {
		    		result = mensTeamPursuit.simulate(strategy, power);
				} catch (Exception e) {
					e.printStackTrace();
				}
				if(result.getProportionCompleted() < 0.9999998){
					double tmp = improvePro(strategy, power, finalTime);
					if(tmp < finalTime)
						finalTime = tmp;
				}
				else{
					if(result.getFinishTime()<finalTime){
						finalTime = result.getFinishTime();
					}
					else{
						power[index] = pValue;
					}
				}				
				times--;
				System.out.println(times);
			}
			if(finalTime<best){
				best = finalTime;
				int i;
				for(i = 0; i < str.length; i++){
					pow[i] = power[i];
					str[i] = strategy[i];
				}
				for(; i< pow.length; i++){
					pow[i] = power[i];
				}
			}
			System.out.println(best);
			print(str,pow);
			for(int i = 0; i< strategy.length; i++){
				strategy[i] = r.nextBoolean();
			}
			for(int i = 0; i< power.length; i++){
				power[i] = 550;
			}
			outtime--;
        }

	}
	
	public double improvePro(boolean[] str, int[] pow, double ftime){
		boolean[] st = new boolean[str.length];
		int[] po = new int[pow.length];
		int i;
		for(i = 0; i < str.length; i++){
			po[i] = pow[i];
			st[i] = str[i];
		}
		for(; i< pow.length; i++){
			po[i] = pow[i];
		}
		
		double proportion;
    	SimulationResult result = null;
        MensTeamPursuit mensTeamPursuit = new MensTeamPursuit();
    	try {
    		result = mensTeamPursuit.simulate(str, pow);
		} catch (Exception e) {
			e.printStackTrace();
		}
		proportion = result.getProportionCompleted();
		
		int times = 50;
		int index, pValue;
		Random r = new Random();
		while(times>0){
			index = r.nextInt(po.length);
			pValue = po[index];
			po[index] = r.nextInt(1001)+200;
	    	try {
	    		result = mensTeamPursuit.simulate(str, pow);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(result.getProportionCompleted()<0.9999998){
				if(result.getProportionCompleted() > proportion){
					proportion = result.getProportionCompleted();
				}
				else{
					po[index] = pValue;
				}
				times--;
			}
			else{
				if(result.getFinishTime()<ftime){
					for(i = 0; i < str.length; i++){
						pow[i] = po[i];
						str[i] = st[i];
					}
					for(; i< pow.length; i++){
						pow[i] = po[i];
					}
					return result.getFinishTime();
				}
				return ftime;
			}
		}
		return ftime;
	}
	
	
	public void print(boolean[] str, int[] pow){
    	SimulationResult result = null;
        MensTeamPursuit mensTeamPursuit = new MensTeamPursuit();
    	try {
    		result = mensTeamPursuit.simulate(str, pow);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(result.toString());
		for(int i =0; i < str.length; i++){
			System.out.print(str[i]+" ");
		}
		System.out.println();
		for(int i =0; i < pow.length; i++){
			System.out.print(pow[i]+" ");
		}
	}

}